Working with Virtual Directories
A
common requirement within Web sites is to include content from folders
that are located outside of the Web site’s primary folder structure.
For example, multiple Web sites that share the same set of images might
need to access a pointer to a single path from which they can all
access files. Virtual directories are designed to provide this
capability. Virtual directories can be created at either the level of a
Web site or within a specific Web application. They include an alias
name (which will be used in the requesting URL) and point to a physical
file system location path.
Creating a Virtual Directory
The
process of creating a virtual directory is similar to that of creating
a Web application. In IIS Manager, right-click the appropriate parent
Web site or Web application and then select Add Virtual Directory. You
will be able to provide an alias for the virtual directory (such as
Images), along with security credentials and the physical path to the
virtual directory. When a request is received for this alias, IIS will
look in the appropriate file system location automatically for the
requested content.
Comparing Virtual Directories and Web Applications
Although
the settings for a virtual directory are similar to those of a Web
application, there are some differences in their usage. Web
Applications are generally designed to support executable Web code such
as applications built using ASP.NET. They run within an isolated
process space, using WAS. The reliance on WAS also enables Web
applications to respond using protocols other than HTTP and HTTPS
(assuming that other protocols have been installed and configured on
the local server).
Virtual
directories, on the other hand, are primarily used to point to static
content that is stored in an alternate file system location. Both Web
applications and virtual directories form a portion of the complete URL
that is used to access a Web site. They can also both be nested to
provide access to multiple levels of site content. The more appropriate
choice will be based on the requirements of the Web application that
you plan to support.
More Info: Keeping the configuration simple
Web
applications and virtual directories offer a lot of power and
flexibility for both Web server administrators and Web developers. In
general, try to keep your configurations as simple and intuitive as
possible. For example, although both types of objects can be nested
within each other, complex nesting can be confusing (especially if some
of the objects share the same names). Overall, keep management of the
Web site in mind when creating and designing the site structure.
Using Command-Line Management
Performing
simple administrator tasks on a few IIS servers is a relatively simple
process, using the IIS Manager console. However, when you want to
commit the same changes on many different servers, or you want to
automate the configuration process by using scripting, command-line
utilities can make these tasks more efficient. IIS includes an
executable command, AppCmd.exe, which provides a simple way for systems
administrators to perform common operational tasks. The actual
parameters are designed to map to the structure of IIS Web sites, Web
applications, and virtual directories.
The AppCmd.exe file is located within the %SystemRoot%\System32\Inetsrv folder. You can get initial help for the utility by running the command with the -/? switch. (See Figure 18.) You can use the same switch to get additional details about other commands. The general syntax for the command is:
AppCmd.exe Command Object "ObjectName" /parameter:value
Understanding Command Options
AppCmd has been designed to use a simple set of six commands for performing tasks on objects. The list of commands includes:
List Returns information about the specified object.
Add Creates a new object of the type that is specified. Details can be added, using parameters and values.
Delete Deletes the specified object (such as a Web site or Web application).
Set Changes settings for the object, as specified by the parameters and values.
Start / Stop Available for objects that support these actions (such as a Web site).
If
you want to perform multiple operations (either from a script file or
from the command line), you will need to call AppCmd.exe for each
operation. This helps keep the syntax of the statements simple and easy
to read.
Understanding Objects
In
a standard AppCmd statement, you will need to provide an object type
and the name of the object that you plan to use. The types of objects
supported by AppCmd.exe include the following:
App (Web Application)
AppPool (Application Pool)
Backup (Server configuration backups)
Config (Server configuration information)
Module
Request
Site (Web Site)
Trace
VDir (Virtual Directory)
WP (Worker Process)
You can get more information about the parameters and values that apply to an object by typing -? after the command.
Examples of Commands
The process of listing, creating, and managing IIS configuration settings by using AppCmd is generally fairly simple. Table 1 provides some examples of common commands and their purpose.
Table 1. Sample Commands for AppCmd.exe
Command | Purpose |
---|
AppCmd list site | Returns a list of Web sites on the local server |
AppCmd add site /name:TestSite01 | Adds a new Web site called TestSite01 |
AppCmd add vdir /app.name:"Default Web Site/" /path:/Images /physicalPath:"C:\Inetpub\wwwroot\images"
| Adds a new virtual directory with the alias Images and points to the specified physical file system location |
AppCmd list request | Returns a list of currently running Web server requests |
AppCmd list config | Returns the entire contents of the current Web server configuration in XML format |
Using Windows PowerShell
In addition to using the AppCmd utility, Web server administrators can use the command shell and scripting language, Microsoft Windows PowerShell.
Windows Server 2008 includes Windows PowerShell as a feature, but it is
not enabled by default. To enable Windows PowerShell, open Server
Manager, right-click Features, and select Add Feature. Select the
Windows PowerShell option, and then click Next to finish installing it.
Once it has been installed, you can launch it from within the Windows
PowerShell 1.0 program group in the Start menu. Windows PowerShell
enables you to write and create powerful scripts for performing many
common administration operations.
More Info: Learning Windows PowerShell
Although
a complete description of how to use PowerShell is beyond the scope of
this book (and Exam 70-643), you can find more information about using
it to manage IIS by searching for Powershell at http://www.iis.net. The Microsoft TechNet Scripting with Windows PowerShell Web site offers tutorials and examples for creating new scripts at http://www.microsoft.com/technet/scriptcenter/hubs/msh.mspx.
Automation Using .NET Framework
Many
Web developers already have a significant amount of knowledge about
working with the .NET Framework. Therefore, it can be helpful for them
to manage IIS by using standard .NET code. IIS 7.0 provides two .NET
namespaces that can be used to manage IIS configuration settings
programmatically. They are:
Microsoft.Web.Administration
This namespace provides objects and methods that are useful for
managing and changing Web server settings. It is focused primarily on
performing configuration changes for an IIS Web server.
Microsoft.Web.Management Although
the default IIS Manager user interface has been designed to provide
simple access to the majority of commonly used functionality, some
environments might want to create their own management extensions for
performing specific tasks. The Microsoft.Web.Management
namespace includes objects and methods that enable developers to extend
the user interface functionality of IIS management tools. These
additions can then be configured to run in a standalone environment, or
they can be integrated with the built-in IIS Manager utility for easy
access.
Understanding
how to write applications by using the .NET Framework is beyond the
scope of Exam 70-643, but it can be helpful to know that these options
are available for automating configuration and management tasks.
Additional information about the namespaces mentioned here and others
can be found at http://msdn2.microsoft.com/en-us/library/aa388745.aspx.